package example.writable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

    private FlowBean flowBean = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Reducer<Text, FlowBean, Text, FlowBean>.Context context)
            throws IOException, InterruptedException {

        Long down = 0L;
        Long up = 0L;
        Long sum = 0L;

        for (FlowBean next : values) {
            down += next.getDownFlow();
            up += next.getUpFlow();
            sum += next.getSumFlow();
        }

        flowBean.setUpFlow(up);
        flowBean.setDownFlow(down);
        flowBean.setSumFlow(sum);

        context.write(key, flowBean);
    }
}
